/**
 * 维护页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_OPT_EMPLOYEE_INFO");
    initSelect2();

    $('#file').ace_file_input({
        style:'well',
        btn_choose:'点击选择图片',
        btn_change:null,
        no_icon:'ace-icon fa fa-picture-o',
        thumbnail:'large',
        droppable:true,
        allowExt: ['jpg', 'jpeg', 'png', 'gif'],
        allowMime: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'],
        before_remove:function () {
            return true;
        },
        before_change:function(files, dropped){
            return isImageFile(files);
        }
    });
    $("#saveBtn").click(function(){
        save();
    });
    $("#goBackBtn, #headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/employeeInfo/index");
    });
    initData();
});

function initData(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/employeeInfo/detail",
        data: {
            uniqueNo: $('#uniqueNo').val()
        },
        success: function (data) {
            if(StringUtils.isNotBlank(data.employeeAvatar)){
                var imgPath = springUrl+"/web/fileStorageInfo/download?uniqueNo="+data.employeeAvatar+"&"+new Date().getTime();
                $('#file').ace_file_input('show_file_list', [{type: 'image', name: data.employeeAvatar, path: imgPath}]);
            }
            $("#employeeAvatar").val(data.employeeAvatar);
            $("#employeeAccount").val(data.employeeAccount);
            $("#orgNo").select2("val", data.orgNo);
            $("#employeeName").val(data.employeeName);
            $("#genderCode").select2("val", data.genderCode);
            $("#mobileNo").val(data.mobileNo);
            $("#cardNo").val(data.cardNo);
        },
        error: function(data){
            Alert.error("初始化数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    if($('#editForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        var formData = new FormData();
        formData.append("file", $("#file")[0].files[0]);
        formData.append("uniqueNo", $("#uniqueNo").val());
        formData.append("employeeAvatar", $("#employeeAvatar").val());
        formData.append("employeeAccount", $("#employeeAccount").val());
        formData.append("orgNo", $("#orgNo").val());
        formData.append("orgName", $('#orgNo option:selected').text());
        formData.append("employeeName", $("#employeeName").val());
        formData.append("genderCode", $("#genderCode").val());
        formData.append("genderName", $('#genderCode option:selected').text());
        formData.append("mobileNo", $("#mobileNo").val());
        formData.append("cardNo", $("#cardNo").val());
        jQuery.ajax({
            type: "POST",
            url: springUrl+"/web/employeeInfo/update",
            data: formData,
            async: false,
            processData : false,
            contentType : false,
            success:function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/employeeInfo/index");
                });
            },
            error : function(data){
                layer.close(index);
                Alert.error(data.responseText);
            }
        });
    }
}